package com.dyb.recuitment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.service.IUserLogService;
import com.dyb.recuitment.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-01-14
 */

@Api(tags = "用户管理(User)")
@RestController
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "获取用户信息(用户ID)", notes = "通过账号ID获取用户信息 {\"id\":1}")
    @ApiImplicitParam()
    @PostMapping("/getUserById")
    public Object getUserById(@RequestBody User user) {
        Object data = null;
        try {
            data = userService.getByUser(user);
            if (data == null) {
                return Result.warning(data, "无此用户!");
            }
            return Result.success(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "获取用户信息(用户名)", notes = "通过用户名获取用户信息 {\"username\":\"\"}")
    @ApiImplicitParam(name = "user", value = "User类")
    @PostMapping("/getByUsername")
    public Object getByUsername(@RequestBody User user) {
        Object data = null;
        try {
            data = userService.getByUser(user);
            if (data == null) {
                return Result.warning(data, "无此用户!");
            }
            return Result.success(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户表单信息 {\"\":\"\",\"id\":1}")
    @ApiImplicitParam(name = "user", value = "User类")
    @PostMapping("/updateUser")
    public Object updateUser(@RequestBody User user) {
        Object data = null;
        try {
            data = userService.updateUser(user);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "修改失败!");
            }
            return Result.success(data, "修改成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "修改密码", notes = "修改用户密码 {\"password\":\"\",\"id\":1}")
    @ApiImplicitParam(name = "user", value = "User类")
    @PostMapping("/updatePwd")
    public Object updatePwd(@RequestBody User user) {
        Object data = null;
        try {
            data = userService.updatePwd(user);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "修改失败!");
            }
            return Result.success(data, "修改成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "禁用用户", notes = "通过用户ID禁用用户 {\"id\":1}")
    @ApiImplicitParam(name = "user", value = "User类")
    @PostMapping("/deleteUser")
    public Object deleteUser(@RequestBody User user) {
        Object data = null;
        try {
            data = userService.deleteUser(user);
            return Result.success(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
